﻿var geoCoder = (function () {
    return {
        getGeoItems: function (coords, metro, house, street, district, country, city) {
            var geoCodeResult = ymaps.geocode(coords, { kind: 'metro' });
            geoCodeResult.then(
                function(res) {
                    result = res.geoObjects.get(0);
                    var name = result.properties.get('name');
                    metro.val(name);
                }
            );
            geoCodeResult = ymaps.geocode(coords, { kind: 'house' });
            geoCodeResult.then(
                function (res) {
                    result = res.geoObjects.get(0);
                    var number = result.properties.get('name').split(',')[1];
                    house.val(number);
                }
            );
            geoCodeResult = ymaps.geocode(coords, { kind: 'street' });
            geoCodeResult.then(
                function (res) {
                    result = res.geoObjects.get(0);
                    var name = result.properties.get('name');
                    var desctiption = result.properties.get('description');
                    var it = desctiption.split(',');
                    street.val(name);
                    country.val(it[0]);
                    city.val(it[1]);
                }
            );
            geoCodeResult = ymaps.geocode(coords, { kind: 'district' });
            geoCodeResult.then(
                function (res) {
                    result = res.geoObjects.get(0);
                    var name = result.properties.get('name');
                    district.val(name);
                }
            );
        }
    }
})();